﻿using System;
using System.Linq;
using System.Linq.Expressions;
using System.Linq.Dynamic;using System.Text;
using System.Data;
using System.Data.Linq;
using System.Transactions;
using LinqToSqlExtensions;using System.ServiceModel;
using System.Collections;
using System.Collections.Generic;
using CardsServer.DAL;
using CardsServer.Tools;
namespace CardsServer.WCF
{
    public partial interface IService
    {
        [OperationContract]
        string GetStations_ByEmpID(Guid empid, int ACID);

        [OperationContract]
        string GetPower_ByStation(string StationCode, int ACID);

        [OperationContract]
        List<string> GetPower_ByEmpID(Guid empid, int ACID);

        [OperationContract]
        List<Power> Getpower_Bymodelid(int modelid, int ACID);
    }
    public partial class Service : IService
    {
        /// <summary>
        /// 取指定员工的岗位集合
        /// </summary>
        /// <param name="empid"></param>
        /// <param name="ACID"></param>
        /// <returns></returns>
        public string GetStations_ByEmpID(Guid empid,int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                return ctx.Employee.Single(p => p.EmplID == empid).Stations;
            }
        }

        /// <summary>
        /// 取指定模块的权限
        /// </summary>
        /// <param name="modelid"></param>
        /// <param name="ACID"></param>
        /// <returns></returns>
        public List<Power> Getpower_Bymodelid(int modelid, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                return ctx.Power.Where(p => p.modelid == modelid).ToList();
            }
        }

        /// <summary>
        /// 取指定岗位的权限集合
        /// </summary>
        /// <param name="StationCode"></param>
        /// <param name="ACID"></param>
        /// <returns></returns>
        public string GetPower_ByStation(string StationCode,int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                return ctx.Station.Single(p => p.StationCode == StationCode).powers;
            }
        }

        /// <summary>
        /// 取指定员工的权限集合
        /// </summary>
        /// <param name="empid"></param>
        /// <param name="ACID"></param>
        /// <returns></returns>
        public List<string> GetPower_ByEmpID(Guid empid,int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                string stations = ctx.Employee.Single(p => p.EmplID == empid).Stations;
                List<string> r = new List<string>();
                foreach (string s in stations.Split('|').ToList())
                {
                   r.Union(ctx.Station.Single(p => p.StationCode == s).powers.Split('|').ToList());
                }
                return r;
            }
        }
    }


}

